package com.iye.puretennis.activity.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import com.iye.puretennis.R;

import java.util.HashMap;
import java.util.Map;

/**
 * Author: Chuanbao on 2014/11/17.
 */
public class SeekBarActivity extends Activity {
    private static final String TAG = "SeekBarActivity";
    private final static Map<Integer,Integer> seekbarLevels = new HashMap<Integer,Integer>();
    SeekBar sb;
    TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.seekbar);



        sb=(SeekBar)findViewById(R.id.seekbar);
        sb.setProgress(0);
        sb.setOnSeekBarChangeListener(sbLis);
    }

    static{
        seekbarLevels.put(0, 1);
        seekbarLevels.put(25, 2);
        seekbarLevels.put(50, 3);
        seekbarLevels.put(75, 4);
        seekbarLevels.put(100, 5);
    }
    private SeekBar.OnSeekBarChangeListener sbLis=new SeekBar.OnSeekBarChangeListener(){

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                                      boolean fromUser) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // 开始拖动时触发，与onProgressChanged区别在于onStartTrackingTouch在停止拖动前只触发一次
            //而onProgressChanged只要在拖动，就会重复触发
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            //结束拖动时触发
            int progress = sb.getProgress();
            MySeekBar mySeekBar = new MySeekBar(progress);
            int nextProgressPoint = mySeekBar.getNextPoint();
            sb.setProgress(nextProgressPoint);

            Toast.makeText(SeekBarActivity.this, seekbarLevels.get(sb.getProgress()) + " 人", Toast.LENGTH_SHORT).show();
        }

        class MySeekBar{
            private int currentProgress;
            private int total= 100;
            private int interval = 25;

            MySeekBar(int progress) {
                this.currentProgress= progress;
            }

            public int getNextPoint() {
                if(currentProgress == 0){
                    return 0;
                }
                //already passed points
                int passPoints = currentProgress / interval;
                int modProgress = currentProgress % interval;
                if(modProgress == 0){ // 没有余数
                    return passPoints * interval;
                }else{
                    return (passPoints+1)*interval;
                }
            }
        }
    };
}